package com.nft.service.school.controller;

import com.nft.po.NftResponse;
import com.nft.service.school.req.LoginReq;
import com.nft.service.school.req.SysUserReq;
import com.nft.service.school.resp.LoginResp;
import com.nft.service.school.service.AuthService;
import com.nft.service.school.service.SysUserService;
import com.nft.service.school.service.VerifyCodeService;
import com.nft.utils.WxCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/auth")
@Api(tags = "登录验证模块")
@Slf4j
public class AuthController {

    @Autowired
    private VerifyCodeService verifyCodeService;

    @Autowired
    private AuthService authService;

    @Autowired
    private SysUserService userAppService;

    @ApiOperation(value="获取验证码")
    @GetMapping("getPhoneCode")
    public NftResponse<Boolean> getPhoneCode(String phone,int type) {
        return verifyCodeService.sendVerifyCode(phone,type);
    }

    @ApiOperation(value="验证码登录")
    @RequestMapping(value = "/loginByCode", method = RequestMethod.POST)
    public NftResponse<LoginResp> loginByCode(@RequestHeader(value = "userId") String userId,@RequestBody LoginReq loginReq) {
        loginReq.setUserId(userId == "" ? null : Long.valueOf(userId));
        return authService.initAppUser(loginReq);
    }

    @ApiOperation(value="退出登录")
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public NftResponse<Boolean> logout(@RequestHeader("userId") String userId) {
        return authService.logout(Integer.valueOf(userId));
    }

    @ApiOperation(value="获取微信授权")
    @RequestMapping(value = "/getopenid", method = RequestMethod.POST)
    public NftResponse<Boolean> getopenid(@RequestBody LoginReq query) {
        String openId = WxCode.getInstance().getOpenId(query.getCode());
        SysUserReq param = new SysUserReq();
        param.setUserId(Long.valueOf(query.getUserId()));
        param.setOpenId(openId);
        return userAppService.updateUserAppInfo(param);
    }
}
